package com.mars.filter;

import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter{
    //过滤中的所有代码，在过滤特定请求时会执行，例如过滤指定页面
    //必须要让过滤器放行，即doFilter方法


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html");

        System.out.println("执行前");
        //放行请求，如果不写就会被拦截在这里
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("执行后");

    }
    //过滤器随服务器一同启动
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("initializing");
    }
    //过滤器随服务器一同关闭
    @Override
    public void destroy() {
        System.out.println("destorying!!");
    }
}
